<template>
    <div class="cart-box">
        <SimpleHeader :name="'购物车'"></SimpleHeader>
        <div class="cart-body">
            <van-checkbox-group v-model="state.result" @change="groupChange" ref="checkboxGroup">
                <van-swipe-cell v-for="(item,index) in state.list" :key="index" :right-width="50" >
                    <div class="good-item">
                        <van-checkbox :name="item.cartItemId"></van-checkbox>
                        <div class="good-img"><img :src="item.goodsCoverImg"></div>
                        <div class="good-desc">
                            <div class="good-title">
                                <span>{{ item.goodsName }}</span>
                                <span>X{{ item.goodsCount }}</span>
                            </div>
                            <div class="good-btn">
                                <div class="price">￥{{ item.sellingPrice }}</div>
                                <van-stepper 
                                    min="1" 
                                    max="5"
                                    :model-value="item.goodsCount"
                                    :name="item.cartItemId"
                                    async-change
                                    @change="onChange" />
                            </div>
                        </div>
                        
                    </div>
                    
                    <template #right>
                        <van-button 
                            square text="删除" 
                            type="danger" 
                            class="delete-button"
                            @click="deleteGood(item.cartItemId)" />
                    </template>
                </van-swipe-cell>
            </van-checkbox-group>
        </div>
        <van-submit-bar 
            class="submit-all van-hairline--top"
            v-if="state.list.length > 0" 
            :price="total * 100" 
            button-text="结算" 
            @submit="onSubmit">
            <van-checkbox v-model:checked="state.checkAll" @click="allCheck">全选</van-checkbox>
        </van-submit-bar>
        <!-- 空购物车 -->
        <div v-if="!state.list.length" class="empty">
            <img :src="imgEmptyCart" class="empty-cart" alt="">
            <div class="title">购物车空空</div>
            <van-button round color="#1baeae" type="primary" @click="goTo">去选购</van-button>
        </div>
    </div>
    <Navbar></Navbar>
</template>

<script setup>
import { computed, onMounted, reactive } from 'vue';
import SimpleHeader from '../components/SimpleHeader.vue';
import Navbar from '../components/Navbar.vue';
import { getCart,modifyCart ,deleteCartItem} from '@/service/cart'
import { showToast } from 'vant';
import { useRouter } from 'vue-router';
import imgEmptyCart from '@/assets/imgs/empty-cart.png'

const router = useRouter()
const state = reactive({
    result:[],
    list:[],
    checkAll:true
})
const total = computed(()=>{
    let sum = 0
    let _list = state.list.filter(item => state.result.includes(item.cartItemId))
    _list.forEach(item=>{
        sum += item.goodsCount * item.sellingPrice
    })
    return sum
})
onMounted(()=>{
    init()
})
// 初始化数据
const init = async ()=>{
    const { data } = await getCart({pageNumber:1})
    // console.log(data);
    state.list = data
    state.result = data.map(item => item.cartItemId)
}
// 选中
const groupChange = (res)=>{
    // console.log('res',res);
    if(res.length == state.list.length) {
        state.checkAll = true
    } else {
        state.checkAll = false
    }
    state.result = res
}
// 改变步进器
const onChange = async (value,detail)=>{
    console.log(value);
    console.log(detail);
    if(value > 5){
        showToast('买超了')
        return
    }
    if(value < 1) {
        showToast('商品个数不小于0')
        return
    }
    state.list.forEach(item=>{
        if(item.cartItemId == detail.name) {
            item.goodsCount = value
        }
    })
    // 去修改服务器中商品数据
    await modifyCart({
        cartItemId: detail.name,
        goodsCount: value
    })
}
// 删除商品
const deleteGood = async (id)=>{
    await deleteCartItem(id)
    init()
}
// 全选
const allCheck = ()=>{
    if(!state.checkAll) {
        state.result = state.list.map(item => item.cartItemId)
    } else {
        state.result = []
    }
}
// 结算
const onSubmit = ()=>{
    if(state.result.length === 0) {
        showToast('请选择商品结算')
        return
    }
    router.push({path:'/create-order',query:{cartItemIds:JSON.stringify(state.result)}})
}

// 去选购goTo
const goTo = ()=>{
    router.push({path:'/home'})
}
</script>

<style lang="less" scoped>
  @import '../common/style/mixin';
  .cart-box {
    .cart-header {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 10000;
      .fj();
      .wh(100%, 44px);
      line-height: 44px;
      padding: 0 10px;
      .boxSizing();
      color: #252525;
      background: #fff;
      border-bottom: 1px solid #dcdcdc;
      .cart-name {
        font-size: 14px;
      }
    }
    .cart-body {
      margin: 16px 0 100px 0;
      padding-left: 10px;
      .good-item {
        display: flex;
        .good-img {
          img {
            .wh(100px, 100px)
          }
        }
        .good-desc {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          flex: 1;
          padding: 20px;
          .good-title {
            display: flex;
            justify-content: space-between;
          }
          .good-btn {
            display: flex;
            justify-content: space-between;
            .price {
              font-size: 16px;
              color: red;
              line-height: 28px;
            }
            .van-icon-delete {
              font-size: 20px;
              margin-top: 4px;
            }
          }
        }
      }
      .delete-button {
        width: 50px;
        height: 100%;
      }
    }
    .empty {
      width: 50%;
      margin: 0 auto;
      text-align: center;
      margin-top: 200px;
      .empty-cart {
        width: 150px;
        margin-bottom: 20px;
      }
      .van-icon-smile-o {
        font-size: 50px;
      }
      .title {
        font-size: 16px;
        margin-bottom: 20px;
      }
    }
    .submit-all {
      margin-bottom: 64px;
      .van-checkbox {
        margin-left: 10px
      }
      .van-submit-bar__text {
        margin-right: 10px
      }
      .van-submit-bar__button {
        background: @primary;
      }
    }
    .van-checkbox__icon--checked .van-icon {
      background-color: @primary;
      border-color: @primary;
    }
  }
</style>